package Pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class CounselorPage extends GuestPage{
    //点击新增
    public CounselorPage clickAdd(){
        waitForWindow(2000);
        driver.findElement(By.cssSelector(".addBtn")).click();
        return this;
    }
    //点击修改 默认第一个
    public CounselorPage clickEdit(){
        waitForWindow(2000);
        driver.findElement(By.cssSelector(".el-table__row:nth-child(1) .el-button:nth-child(1)")).click();
        return this;
    }
    /** 直接通过findElement方法获取某个元素，如果该元素不存在肯定会报错，
     *  selenium又没有可以判断该元素是否存在的方法，
     *  于是我们可以手写一个工具类，来判断这个元素是否存在
     *  判断某个元素是否存在,存在返回true,否则false
     */
    public boolean isElementExist(WebDriver driver, By by) {
        try {
            WebElement element = driver.findElement(by);
            //System.out.println(element.getText());
            return true;
        } catch (Exception e) {
            //System.out.println("不存在此元素");
            return false;
        }
    }

    //输入姓名
    public CounselorPage inputName(String keys){
        waitForWindow(2000);
        driver.findElement(By.xpath("(//input[@type='text'])[4]")).click();
        driver.findElement(By.xpath("(//input[@type='text'])[4]")).sendKeys(keys);
        return this;
    }
    //修改姓名
    public CounselorPage editName(String keys){
        waitForWindow(2000);
        //点击clear
        //.el-icon-circle-close
        driver.findElement(By.xpath("(//input[@type='text'])[4]")).click();
        driver.findElement(By.cssSelector(".el-icon-circle-close")).click();
        //重新输入
        inputName(keys).confirmAdd();
        return this;
    }
    //输入电话号码
    public CounselorPage inputPhoneNum(String keys){
        waitForWindow(2000);
        driver.findElement(By.xpath("(//input[@type='text'])[5]")).click();
        driver.findElement(By.xpath("(//input[@type='text'])[5]")).sendKeys(keys);
        return this;
    }
    //修改电话
    public CounselorPage editPhoneNum(String keys){
        waitForWindow(2000);
        //点击clear
        driver.findElement(By.xpath("(//input[@type='text'])[5]")).click();
        driver.findElement(By.cssSelector(".el-icon-circle-close")).click();
        //重新输入
        inputPhoneNum(keys).confirmAdd();
        return this;
    }


    //输入身份证号
    public CounselorPage inputID(String keys){
        waitForWindow(2000);
        driver.findElement(By.xpath("(//input[@type='text'])[6]")).click();
        driver.findElement(By.xpath("(//input[@type='text'])[6]")).sendKeys(keys);
        return this;
    }
    //修改身份证号
    public CounselorPage editID(String keys){
        waitForWindow(2000);
        //点击clear
        driver.findElement(By.xpath("(//input[@type='text'])[6]")).click();
        driver.findElement(By.cssSelector(".el-icon-circle-close")).click();
        //重新输入
        inputID(keys).confirmAdd();
        return this;
    }

    //输入邮箱
    public CounselorPage inputEMail(String keys){
        waitForWindow(2000);
        driver.findElement(By.xpath("(//input[@type='text'])[7]")).click();
        driver.findElement(By.xpath("(//input[@type='text'])[7]")).sendKeys(keys);
        return this;
    }
    //修改邮箱
    public CounselorPage editEmail(String keys){
        waitForWindow(2000);
        //点击clear
        driver.findElement(By.xpath("(//input[@type='text'])[7]")).click();
        driver.findElement(By.cssSelector(".el-icon-circle-close")).click();
        //重新输入
        inputEMail(keys).confirmAdd();
        return this;
    }


    //绑定督导
    public CounselorPage selectSupervisor(String keys){
        waitForWindow(2000);
        //空则不选择督导
        if(keys == "") {
        }
        //选择第一个督导
        else if(keys == "1"){
            //下拉
            driver.findElement(By.xpath("(//input[@type='text'])[8]")).click();
            /*driver.findElement(By.xpath("(//input[@type='text'])[8]")).click();*/
            driver.findElement(By.cssSelector("div.el-select-dropdown:nth-child(11) > div:nth-child(1) > div:nth-child(1) > ul:nth-child(1) > li:nth-child(1)")).click();
            //driver.findElement(By.xpath("//li[contains(.,'一级')]")).click();
        }
        //选择第二个督导
        else if(keys == "2"){
            //下拉
            driver.findElement(By.xpath("(//input[@type='text'])[8]")).click();
            driver.findElement(By.xpath("(//input[@type='text'])[8]")).click();
            driver.findElement(By.cssSelector("div.el-select-dropdown:nth-child(11) > div:nth-child(1) > div:nth-child(1) > ul:nth-child(1) > li:nth-child(2)")).click();
        }
        return this;
    }


    //输入工作单位
    public CounselorPage inputWorkPlace(String keys){
        waitForWindow(2000);
        driver.findElement(By.xpath("(//input[@type='text'])[10]")).click();
        driver.findElement(By.xpath("(//input[@type='text'])[10]")).sendKeys(keys);
        return this;
    }
    //修改工作单位
    public CounselorPage editWorkplace(String keys){
        waitForWindow(2000);
        //点击clear
        driver.findElement(By.xpath("(//input[@type='text'])[10]")).click();
        driver.findElement(By.cssSelector(".el-icon-circle-close")).click();
        //重新输入
        inputWorkPlace(keys).confirmAdd();
        return this;
    }

    //输入职称
    public CounselorPage inputJobTitle(String keys){
        waitForWindow(2000);
        driver.findElement(By.xpath("(//input[@type='text'])[11]")).click();
        driver.findElement(By.xpath("(//input[@type='text'])[11]")).sendKeys(keys);
        return this;
    }
    //修改职称
    public CounselorPage editJobTitle(String keys){
        waitForWindow(2000);
        //点击clear
        driver.findElement(By.xpath("(//input[@type='text'])[11]")).click();
        driver.findElement(By.cssSelector(".el-icon-circle-close")).click();
        //重新输入
        inputJobTitle(keys).confirmAdd();
        return this;
    }

    //输入用户名
    public CounselorPage inputUsername(String keys){
        waitForWindow(2000);
        driver.findElement(By.xpath("(//input[@type='text'])[12]")).click();
        driver.findElement(By.xpath("(//input[@type='text'])[12]")).sendKeys(keys);
        return this;
    }
    //修改用户名
    public CounselorPage editUsername(String keys){
        waitForWindow(2000);
        //点击clear
        driver.findElement(By.xpath("(//input[@type='text'])[12]")).click();
        driver.findElement(By.cssSelector(".el-icon-circle-close")).click();
        //重新输入
        inputUsername(keys).confirmAdd();
        return this;
    }
    //输入密码
    public CounselorPage inputPassword(String keys){
        waitForWindow(2000);
        driver.findElement(By.xpath("//input[@type='password']")).click();
        driver.findElement(By.xpath("//input[@type='password']")).sendKeys(keys);
        return this;
    }
    //修改用户名
    public CounselorPage editPassword(String keys){
        waitForWindow(2000);
        //重新输入
        inputPassword(keys).confirmAdd();
        return this;
    }
    //新增督导 输入信息的方法 10个参数
    public String inputAll(String name, String phoneNum, String ID,
                                   String Email,String quality,String workplace, String jobTitle,
                                     String username, String password){
        inputName(name).
        inputPhoneNum(phoneNum).
        inputID(ID).
        inputEMail(Email).
        selectSupervisor(quality).
        inputWorkPlace(workplace).
        inputJobTitle(jobTitle).
        inputUsername(username).
        inputPassword(password);
        confirmAdd(); //最后点击确认添加
        String info = getErrorMessage();
        return info;
    }

    /**
     * 用于维护 所有错误提示信息的输出，重写getErrorMessage
     * 因为有10个输入框，因此有10个错误提示消息的维护
     * @return
     */
    @Override
    public String getErrorMessage() {
        waitForWindow(2000);
        String info = "";
        //name错误提示消息的css选择器
        By.ByCssSelector nameError = new By.ByCssSelector(".el-row:nth-child(1) > .el-col:nth-child(1) .el-form-item__error");
        if(isElementExist(driver,nameError)){
            WebElement element = driver.findElement(nameError);
            waitForWindow(500);
            String text = "姓名：" + element.getText();
            info = info + (text + '\n');
        }

        //phoneNum错误提示消息的css选择器
        By.ByCssSelector phoneNumError = new By.ByCssSelector(".el-row:nth-child(1) > .el-col:nth-child(2) .el-form-item__error");
        if(isElementExist(driver,phoneNumError)){
            WebElement element = driver.findElement(phoneNumError);
            waitForWindow(500);
            String text = "电话：" + element.getText();
            info = info + (text + '\n');
        }

        //ID错误提示消息的css选择器
        By.ByCssSelector IDError = new By.ByCssSelector(".el-row:nth-child(2) .el-form-item__error");
        if(isElementExist(driver,IDError)){
            WebElement element = driver.findElement(IDError);
            waitForWindow(500);
            String text = "身份证号：" + element.getText();
            info = info + (text + '\n');
        }

        //Email错误提示消息的css选择器
        By.ByCssSelector EmailError = new By.ByCssSelector(".el-row:nth-child(3) > .el-col:nth-child(1) .el-form-item__error");
        if(isElementExist(driver,EmailError)){
            WebElement element = driver.findElement(EmailError);
            waitForWindow(500);
            String text = "邮箱：" + element.getText();
            info = info + (text + '\n');
        }

        //selectSupervisor错误提示消息的css选择器
        By.ByCssSelector JobTitleError = new By.ByCssSelector(".el-row:nth-child(3) > .el-col:nth-child(2) .el-form-item__error");
        if(isElementExist(driver,JobTitleError)){
            WebElement element = driver.findElement(JobTitleError);
            waitForWindow(500);
            String text = "绑定督导：" + element.getText();
            info = info + (text + '\n');
        }

        //workSpace错误提示消息的css选择器
        By.ByCssSelector workSpaceError = new By.ByCssSelector(".el-row:nth-child(4) > .el-col:nth-child(1) .el-form-item__error");
        if(isElementExist(driver,workSpaceError)){
            WebElement element = driver.findElement(workSpaceError);
            waitForWindow(500);
            String text = "工作单位：" +element.getText();
            info = info + (text + '\n');
        }

        //QualityNum错误提示消息的css选择器
        By.ByCssSelector jobTitleError = new By.ByCssSelector(".el-row:nth-child(4) > .el-col:nth-child(2) .el-form-item__error");
        if(isElementExist(driver,jobTitleError)){
            WebElement element = driver.findElement(jobTitleError);
            waitForWindow(500);
            String text = "职称：" +element.getText();
            info = info + (text + '\n');
        }



        //Username错误提示消息的css选择器
        By.ByCssSelector UsernameError = new By.ByCssSelector(".el-row:nth-child(5) .el-form-item__error");
        if(isElementExist(driver,UsernameError)){
            WebElement element = driver.findElement(UsernameError);
            waitForWindow(500);
            String text = "用户名：" +element.getText();
            info = info + (text + '\n');
        }

        //Password错误提示消息的css选择器
        By.ByCssSelector PasswordError = new By.ByCssSelector(".el-row:nth-child(6) .el-form-item__error");
        if(isElementExist(driver,PasswordError)){
            WebElement element = driver.findElement(PasswordError);
            waitForWindow(500);
            String text = "密码：" +element.getText();
            info = info + (text + '\n');
        }
        waitForWindow(2000);
        return info;
    }

    public CounselorPage confirmAdd() {
        driver.findElement(By.cssSelector(".el-dialog__footer:nth-child(3) .confirm")).click();
        return this;
    }

    public CounselorPage cancelAdd(){
        driver.findElement(By.cssSelector(".el-dialog__footer:nth-child(3) .cancel")).click();
        return this;
    }

}
